package com.foreknow.controller;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //我们只需要在doGet方法中获取请求参数，然后根据获取到参数来决定调用哪一个方法
        String method = req.getParameter("q");
        System.out.println(req.getParameter("username"));
        if("delete".equals(method)){
            delete(req,resp);
        }else if("update".equals(method)){
            update(req,resp);
        }else if ("updatelist".equals(method)){
            updatelist(req,resp);
        }else {
            list(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有的留言信息
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        //调用业务层查询所有留言信息的方法
        GuestbookService service = new GuestbookServiceImpl();
        List<Object> list = service.queryInfo();
        //将查询到的list集合保存到服务器端
        req.setAttribute("list",list);
        //就可以将这个请求转发给list.jsp
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除留言信息
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        GuestbookService service = new GuestbookServiceImpl();
        String id = req.getParameter("id");
        boolean isRight = service.deleteBean(Integer.parseInt(id));
        if(isRight){
            req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
        }

    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //修改留言信息
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        //调用业务层修改留言信息的方法
        GuestbookService service = new GuestbookServiceImpl();
        String id = req.getParameter("id");
        List<Object> list = service.getById(Integer.parseInt(id));
        Guestbook gb=(Guestbook) list.get(0);
        boolean isRight = service.update(gb);
        if (isRight){
            req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
        }
    }
    protected void updatelist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据id查询的留言信息
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        //调用业务层根据id查询某一条留言信息的方法
        GuestbookService service = new GuestbookServiceImpl();
        String id = req.getParameter("id");
        List<Object> list = service.getById(Integer.parseInt(id));
        //将查询到的list集合保存到服务器端
        req.setAttribute("updatelist",list);
        //就可以将这个请求转发给updatelist.jsp
        req.getRequestDispatcher("/updatelist.jsp").forward(req,resp);
    }


}
